package com.thinksns.sociax.t4.android.presenter;


import android.os.Handler;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.gift.BindAccountActivity;
import com.thinksns.sociax.t4.android.view.BindAccountView;
import com.thinksns.sociax.t4.unit.UiUtils;
import com.thinksns.sociax.thinksnsbase.network.ApiHttpClient;

/**
 * Created by jess on 16/4/18.
 */
public class BindAccountPresenter {
    private static final String STATUS_BIND = "1";
    private static final String STATUS_UNBIND = "2";
    private Thinksns app = Thinksns.getApplication();
    private BindAccountView mRootView;

    public BindAccountPresenter(BindAccountView rootView) {
        this.mRootView = rootView;

    }

    /**
     * 绑定账户
     *
     * @param account 账户
     * @param type    支付宝还是微信
     */
    public void bindWallet(String account, int type) {
        setUserAccount(STATUS_BIND, account, type);
    }

    /**
     * 解除绑定
     *
     * @param type
     */
    public void unbindWallet(int type) {
        setUserAccount(STATUS_UNBIND, "", type);
    }

    private void setUserAccount(final String status, String account, final int type) {
        app.getApplicationApi().setUserAccount(status, type + "", account, new ApiHttpClient.HttpRespListener<String>() {
            @Override
            public void onSuccess(String result) {
                if (status.equals(STATUS_BIND)) {
                    mRootView.showMessage(UiUtils.getString(R.string.str_bind_success));
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mRootView.killMyself();
                        }
                    }, 1200);
                } else {
                    mRootView.showMessage(UiUtils.getString(R.string.str_unbind_success));
                    changeBindStatus(type);
                }

            }

            @Override
            public void onError(Throwable result) {
                result.printStackTrace();
                mRootView.showMessage(UiUtils.getString(R.string.net_work_error));
            }

            @Override
            public void onFail(int code, String msg) {
                mRootView.showMessage(msg);
            }
        });
    }


    /**
     * 改变绑定状态
     *
     * @param bindType
     */
    private void changeBindStatus(int bindType) {
        if (bindType == BindAccountActivity.ACCOUNT_ALIPAY) {
            mRootView.setAlipayBindStatus(false);
        } else if (bindType == BindAccountActivity.ACCOUNT_WEICHAT) {
            mRootView.setWeiChatBindStatus(false);
        }
        mRootView.setBindStatus();//设置是否显示解绑按钮
    }


}
